package com.mbti.youmore.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mbti.youmore.common.exception.PubBannedWordsException;
import com.mbti.youmore.common.exception.UserAlreadyExistsException;
import com.mbti.youmore.common.exception.UserNotExistsException;
import com.mbti.youmore.entity.User;
import com.mbti.youmore.mapper.UserMapper;
import com.mbti.youmore.service.AuthService;
import com.mbti.youmore.util.AccessTokenUtils;
import com.mbti.youmore.util.BannedWordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

/**
 * @author Guqier
 * @version 1.0
 * @date 2024/5/8 下午8:41
 */
@Service
public class AuthServiceImpl implements AuthService {


    @Autowired
    private UserMapper userMapper;// 使用Spring自动注入工具类

    /**
     * 用户注册
     *
     * @param user 用户对象
     * @return 注册成功的用户对象
     * @throws UserAlreadyExistsException 用户已存在异常
     * @throws IOException                IO异常
     */
    @Override
    @Transactional // 启用事务
    public User register(User user) throws UserAlreadyExistsException, IOException {
        // 敏感词监测
        if(BannedWordUtil.isBannedWord(user.getUsername()) || BannedWordUtil.isBannedWord(user.getNickname())){
            throw new PubBannedWordsException("昵称或用户名不合法");
        }

        // 检查用户名是否存在
        User target = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, user.getUsername()));
        if (target != null) {
            throw new UserAlreadyExistsException("用户名已存在！");
        }

        // 添加用户属性的默认值
        user.setUserLang("简体中文");
        user.setUserLocation("四川省");
        user.setProfilePic("http://8.137.16.7:9000/youmore/30b9b7bb-5084-4143-94c5-85651518c61c.png");
        user.setCoverPic("http://8.137.16.7:9000/youmore/6bc15448-bbec-421e-8ce4-65c2cf828eb2.jpg");
        user.setIntegral(0);
        int count = userMapper.insert(user);
        if (count == 1) {
            return user;
        }
        return null;
    }

    /**
     * 用户登录
     *
     * @param user 用户对象
     * @return 登录成功的用户对象
     * @throws UserNotExistsException 用户不存在异常
     */
    @Override
    @Transactional
    public User login(User user) throws UserNotExistsException {
        User target = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, user.getUsername()));
       if (target == null) { // 用户不存在
            throw new UserNotExistsException("用户不存在！");
        }

        if (user.getPassword().equals(target.getPassword())) {
            StpUtil.login(target.getUserId());
            AccessTokenUtils.setUserSession(target);
            return target;
        }
        throw new UserNotExistsException("账号或密码错误！");
    }

    /**
     * 用户登出
     *
     * @param userId 用户ID
     * @return 登出结果
     */
    @Override
    @Transactional
    public boolean logout(Integer userId) {
        StpUtil.logout();
        return true;
    }

}
